#ifndef PIPE_H
#define PIPE_H

#include <Windows.h>
#include <string>
#include <QObject>

class MainWindow;

class CPipe : public QObject
{
  Q_OBJECT

public:
  static const int INVALID_COMMAND {0};
  static const int SHOW_MAIN_WINDOW {1};

  CPipe();
  ~CPipe();

  void Stop();
  int Run();
  void SetMainWindow(MainWindow *main_window);

signals:
  void showWindowNormal();

private:
  std::wstring pipe_name_{LR"raw(\\.\pipe\__tomato_pipe__)raw"};
  volatile int stop_{0};
  HANDLE pipe_handle_{nullptr};
  uintptr_t worker_handle_{0llu};
  MainWindow *main_window_{nullptr};

  int CreateTheNamedPipe();
  int WriteCommandToServier();
  int HandleConnection();
  unsigned int doWaitForClient();
  static unsigned int __stdcall WaitForClientThread(void *arg);
};

#endif // PIPE_H
